package com.mililoan.console.service;

import java.util.Date;
import java.util.HashMap;

import com.mililoan.common.bean.UserStatus;
import com.mililoan.common.utils.Tool;
import com.mililoan.console.bean.SysConfig;


/**
 * 用户登录失败次数处理
 * @author hozen 2014-6-26
 */
public class LoginFailService {

	private static HashMap statusHm = new HashMap();;
	
	/**
	 * 读取所有状态的HashMap列表
	 * @since hozen 2014-6-26 
	 * @return
	 */
    public synchronized static HashMap getStatusHm(){
        return statusHm;
    }

    /**
     * 读取状态
     * @since hozen 2014-6-26 
     * @param userName
     * @return
     */
    public static UserStatus getStatus(String userName ){
    	UserStatus status= (UserStatus)getStatusHm().get(userName);
        return status;
    }

    /**
     * 增加登录错误次数
     * @since hozen 2014-6-26 
     * @param userName
     * @return
     */
    public synchronized static UserStatus addFailTime(String userName){

        UserStatus status = (UserStatus)getStatusHm().get(userName);
        if(status==null){
            status = new UserStatus();
            status.setUserName(userName);
            status.setLoginDate(new Date());
            status.setLoginFailTime(1);
            getStatusHm().put(status.getUserName() + "", status);

        }else{
            status.setLoginFailTime(status.getLoginFailTime()+1);
            
            if(status.getLoginFailTime()==SysConfig.LOGIN_FAIL_MAX_TIME){
            	//System.out.println("##LoginFail"+status.getUserName() +"|" + LOGIN_FAIL_MAX_TIME);
                status.setLogoutDate(new Date());
            }

            status.setTouchDate(new Date());
        }
 	   	System.out.println("#add#"+status.getUserName() +"|" +status.getLoginFailTime() +"|" + Tool.formatDate(status.getLoginDate()) +"|" +Tool.formatDate(status.getLogoutDate()) );

        return status;
    }

    /**
     * 删除状态
     * @since hozen 2014-6-26 
     * @param userName
     * @return
     */
    public static boolean deleteStatus(String userName){
        getStatusHm().remove(userName);
        return true;
    }

    /**
     * 验证最大错误次数
     * @since hozen 2014-6-26 
     * @param userName
     * @return
     */
    public static boolean checkMaxFail(String userName){
    	UserStatus status= (UserStatus)getStatusHm().get(userName);
     	if(status!=null ){
     	   	System.out.println("#check#"+status.getUserName() +"|" +status.getLoginFailTime() +"|" + Tool.formatDate(status.getLoginDate()) +"|" +Tool.formatDate(status.getLogoutDate()) );

    		if(status.getLoginFailTime() >= SysConfig.LOGIN_FAIL_MAX_TIME){//大于5次

	            long remainder = (new Date().getTime()) - status.getLogoutDate().getTime();
	            if(remainder > SysConfig.LOGIN_FAIL_WAIT_TIME *60 * 1000) {//1 minites)
	            	deleteStatus(userName);
	            	return false;
	            }
    			return true;
    		}
    	}
    	return false;
    }

}
